package Model.OtherUser;



import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import Model.User;
import DBAccess.DBAccess;

public class Friends extends OtherUser {
	public int[] friends;

    public Group myGroup;
    public User  myUser;
  
    public Friends(){
    	super();
    	friendblocked=FRIEND;
    }
    
    public static void main(String[] args){
    	new DBAccess();
    	ArrayList<Group> group = getGroups(1);
    	for (int i=0; i<group.size();i++){
    		System.out.println(group.get(i).groupName);
    		for (int j=0;j<group.get(i).users.size();j++)
    			System.out.println("\t"+group.get(i).users.get(j));
    	}
    }

    public static void addFriend(int userID, int friendID, String group){
    	addOtherUser(userID, friendID, FRIEND, group);
    }
    
    public static ArrayList<Group> getGroups(int userID){
    	PreparedStatement ps = null;
    	ResultSet rs = null;
    	ArrayList<Group> groups = new ArrayList<Group>();
    	try{
    		ps = DBAccess.con.prepareStatement("SELECT * FROM otherusersdb WHERE userID=? AND friendblocked=? ORDER BY fgroup");
    		ps.setInt(1, userID);
    		ps.setInt(2, FRIEND);
    		rs = ps.executeQuery();
    		String lastgroup = null;
    		while(rs.next()){
    			String groupname = rs.getString("fgroup");
    			
    			if (!groupname.equals(lastgroup)){
    				Group newgroup = new Group(groupname);
    				groups.add(newgroup);
    			}
    			
    			Group thisgroup = groups.get(groups.size()-1);
    			thisgroup.addToGroup(rs.getInt("otherUserID"));
    			
    			lastgroup = groupname;
    		}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	DBAccess.close(ps, rs);
    	return groups;
    }
    
	public static boolean isFriend(int userID, int otherID){
		return isFriendOrBlocked(userID,otherID,FRIEND);
	}
    
}